//plus api
//创建或者打开数据库
export function openDatabase(name, path) {
	return new Promise((resolve, reject) => {
		plus.sqlite.openDatabase({
			name: name, //数据库名称
			path: `_doc/${path}.db`,
			success() {
				resolve({
					msg: 'success',
					code: 1
				}); //成功回调
			},
			fail(err) {
				resolve({
					msg: `fail :${JSON.stringify(err)}`,
					code: 0
				}); //失败回调
			}
		})
	})
}


//判断数据库是否打开
export function isOpenDatabase(name, path) {
	return new Promise((resolve, reject) => {
		let res = plus.sqlite.isOpenDatabase({
			name: name,
			path: `_doc/${path}.db`
		})
		resolve(res)
	})
}
//关闭数据库链接
export function closeDataBase(name) {
	return new Promise((resolve, reject) => {
		plus.sqlite.closeDataBase({
			name: name, //数据库名称
			success() {
				resolve({
					msg: 'success',
					code: 1
				}); //成功回调
			},
			fail(err) {
				resolve({
					msg: `fail :${err}`,
					code: 0
				}); //失败回调
			}
		})
	})
}
//执行事务
export function transaction(name, operation) {
	return new Promise((resolve, reject) => {
		plus.sqlite.transaction({
			name: name,
			operation: operation,
			success: () => {
				resolve({
					msg: 'success',
					code: 1
				})
			},
			fail: (err) => {

			}
		})
	})
}
//增删改
export function executeSql(obj) {
	console.log('执行语句executeSql')
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: obj.name,
			sql: obj.sql,
			success: (e) => {
				resolve({
					msg: 'success',
					code: 1
				})
			},
			fail: (err) => {
				console.log(err)
				resolve({
					msg: err,
					code: 0
				})
			}
		})
	})
}
//查
export function selectSql(obj) {
	console.log('执行语句selectSql')
	return new Promise((resolve, reject) => {
		plus.sqlite.selectSql({
			name: obj.name,
			sql: obj.sql,
			success: (data) => {
				// console.log(data)
				resolve({
					msg: 'success',
					code: 1,
					data: data
				})
			},
			fail: (err) => {
				console.log(err)
				resolve({
					msg: err,
					code: 0
				})
			}
		})
	})
}
